#include <dspSimulator/instruction/sploopw.h>


bool SPLOOPW::checkCondition(std::shared_ptr<DspMachine> dspMachine) {
    return true;
}

void SPLOOPW::loadInstIRIntoJit(llvm::LLVMContext *llvmContext, llvm::Module *module,
                                std::shared_ptr<llvm::IRBuilder<>> irBuilder) {

}

std::shared_ptr<Instruction> SPLOOPW::decode(std::shared_ptr<InstInfo> instInfo, u64 baseCycle) {
    std::shared_ptr<SPLOOPW> sploopw;
    auto dspInstInfo = std::dynamic_pointer_cast<DspInstInfo>(instInfo);
    bool isStandardInst = dspInstInfo->isStandardInstruction();
    if (isStandardInst) {
        auto data = vectorToBitSet<32>(dspInstInfo->getBits());
        if (checkBits<21>(data, 2, 0b000001111100000000000) && existStandardCondition(data)) {
            sploopw = std::make_shared<SPLOOPW>(dspInstInfo->getAddress(),baseCycle);
            sploopw->creg_z = extractBits<4>(data, 28);
            sploopw->ii = extractBits<5>(data, 23).to_ulong() + 1;
            sploopw->s = data[1];
            sploopw->p = data[0];
        }
    }
    return sploopw;
}

void SPLOOPW::executeCustom(std::shared_ptr<TargetMachine> targetMachine, ExecutePhase executePhase) {

}

std::shared_ptr<DspInstruction> SPLOOPW::clone() {
    return std::make_shared<SPLOOPW>(*this);
}

std::string SPLOOPW::toString() const {
    std::string res;
    if (!isCompactInstruction) {
        res += getStandardConditionString() + " ";
    }
    res += m_name + " " + std::to_string(ii);
    return res;
}
